﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySchool;
using System.Data.SqlClient;

namespace WinForm4
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 清空数据
            this.listView1.Items.Clear();
            // 执行查询
            DBHelper dBHelper = new DBHelper();
            dBHelper.OpenConnection();
            // 获取文本框中的内容
            string content = this.textBox1.Text.Trim();
            string sql = "";
            if (content.Equals(string.Empty)) {
                // 执行全查询
                sql = "select * from sys_user";
            } else {
                sql = "select * from sys_user where username like '%"+content+"%'";
            }
            // 创建sqlcommand
            SqlCommand sqlCommand = new SqlCommand(sql, dBHelper.Connection);
            SqlDataReader reader = sqlCommand.ExecuteReader();
            // 循环附加数据
            while (reader.Read()) {
                ListViewItem item = new ListViewItem(reader["id"].ToString());
                item.SubItems.Add(reader["username"].ToString());
                this.listView1.Items.Add(item);
            }
            reader.Close();
            dBHelper.CloseConnection();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
        

            
        }
    }
}
